package cn.wgx.module.file.web;


import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.file.entity.FileInfo;
import cn.wgx.module.file.service.imp.FastDFSServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Api(tags = "1.0 文件上传")
@RestController
@RequestMapping("/api/file")
public class FileCtrl {

    //    @Autowired
//    FileService fileService;
    @Autowired
    FastDFSServiceImpl fileService;

    @ApiOperation("文件列表")
    @GetMapping("list")
    public Map findFiles(@ModelAttribute FileInfo fileInfo,
                         @RequestParam(required = false, defaultValue = "1") Integer page,
                         @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, fileService.page(page1, new QueryWrapper<>(fileInfo)).getRecords());
    }

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Map upload(@RequestParam("file") MultipartFile file,@RequestParam(required = false) String md5) throws Exception {
        return ResponseDataUtil.returnData(fileService.upload(file,md5));
    }

    @ApiOperation("获取文件详细信息,id就是文件MD5值")
    @GetMapping("get")
    public Map getById(@RequestParam String id) {
        return ResponseDataUtil.returnData(fileService.getById(id));
    }

}
